package com.llb666.e.shop.domain.service;

import com.llb666.e.shop.domain.entity.UserAddress;
import com.llb666.e.shop.domain.repository.UserAddressRepository;
import lombok.RequiredArgsConstructor;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 斌~
 * @date 2022/9/11 下午11:14
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserAddressService {

    private final UserAddressRepository repository;
    private final Sid sid;

    /**
     * 用户地址列表查询.
     *
     * @param userId 用户Id
     */
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public List<UserAddress> queryAll(String userId) {
        return repository.selectByUserId(userId);
    }

    /**
     * 添加收获地址.
     *
     * @param userAddress 表单收获地址
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addUserAddress(UserAddress userAddress) {
        // 判断是不是第一次添加收获地址，如果是，设为为默认收获地址
        List<UserAddress> userAddresses = this.queryAll(userAddress.getUserId());
        if (CollectionUtils.isEmpty(userAddresses)) {
            userAddress.setIsDefault(1);
        }
        userAddress.setId(sid.nextShort());
        userAddress.setIsDefault(0);
        userAddress.setCreatedTime(new Date());
        userAddress.setUpdatedTime(new Date());
        repository.addUserAddress(userAddress);
    }


    /**
     * 更新地址.
     *
     * @param userAddress 表单提交的收获地址
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateUserAddress(UserAddress userAddress) {
        userAddress.setUpdatedTime(new Date());
        repository.updateUserAddress(userAddress);
    }

    /**
     * 删除地址.
     *
     * @param userId    用户id
     * @param addressId 收获地址id
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteUserAddress(String userId, String addressId) {
        repository.deleteByUserIdAndAddressId(userId, addressId);
    }

    /**
     * 设置默认地址.
     *
     * @param userId    用户id
     * @param addressId 收获地址id
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateUserAddressToBeDefault(String userId, String addressId) {
        // 1.先把默认地址信息设置为非默认地址（确保只有一个默认地址）
        repository.updateUserAddressToBeNotDefault(userId, addressId);
        // 2. 根据地址id修改为默认的地址
        repository.updateUserAddressToBeDefault(userId, addressId);
    }

    /**
     * 根据用户id和地址id，查询具体的用户地址对象信息
     *
     * @param userId    用户id
     * @param addressId 收获地址id
     * @return UserAddress 收获地址
     */
    public UserAddress queryUserAddress(String userId, String addressId) {
        return repository.selectByUserIdAndAddressId(userId, addressId);
    }
}
